package com.example.hilt_learn

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.hilt_learn.bean.ChinaCar
import com.example.hilt_learn.bean.Fruit
import com.example.hilt_learn.bean.User
import com.example.hilt_learn.bean.engine.Engine
import com.example.hilt_learn.di.anno.MadeInCN
import com.example.hilt_learn.di.anno.MadeInUSA
import com.example.hilt_learn.myadapter.MyAdapter
import com.example.hilt_learn.ui.theme.Hilt_learnTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

private const val TAG = "MainActivity"

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject
    lateinit var user: User

    @Inject
    @MadeInCN
    lateinit var chinaCar: ChinaCar

    @Inject
    @MadeInUSA
    lateinit var chinaCar2: ChinaCar

    @Inject
    @MadeInCN
    lateinit var chinaEngine: Engine

    @Inject
    lateinit var pear: Fruit

    @Inject
    lateinit var myAdapter: MyAdapter

    private val userState by lazy { mutableStateOf(user) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Hilt_learnTheme {
                MainPage(userState.value) {
                    userState.value = it
                    chinaCar.name = "马自达"
                    Log.d(TAG, "chinaCar.name = ${chinaCar.name}")
                    chinaCar.engine.on()
                    chinaCar.engine.off()

                    Log.d(TAG, "engine is same: ${chinaCar.engine == chinaEngine}") // false

                    Log.d(TAG, "pear.name = ${pear.name}")

                    chinaCar2.name = "雪佛兰"
                    Log.d(TAG, "chinaCar2.name = ${chinaCar2.name}")
                    chinaCar2.engine.on()
                    chinaCar2.engine.off()

                    myAdapter.showContext()
                }
            }
        }
    }
}

@Composable
fun MainPage(
    userState: User,
    onUserChange: (User) -> Unit
) {
    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Surface(
            color = MaterialTheme.colors.background
        ) {
            Greeting("${userState.name}, ${userState.age}")
        }
        Button(onClick = {
            User().apply {
                name = "libai"
                age = 30
            }.let {
                onUserChange(it)
            }
        }) {
            Text(text = "Click me")
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Hilt_learnTheme {
        MainPage(User()) {

        }
    }
}